function TMacros:GetBagItemSlot(ItemName)
	for bagID = 0, 4 do
		local NumSlots = GetContainerNumSlots(bagID)
		if NumSlots >= 0 then 
			for slot = 1, NumSlots do 
				if self:GetBagSlotItemName(bagID, slot) == ItemName then 
					return bagID, slot
				end
			end
		end
    end
	return nil, nil
end

function TMacros:GetBagSlotItemInfo(BagId, SlotId)
	local link = GetContainerItemLink(BagId, SlotId)
	if not link then return nil end
	return self:GetItemInfoFromLink(link)
end

function TMacros:UseBagItem(Item, Unit)
	local bag, pos = self:GetBagItemSlot(Item)
	if not bag or not pos then return end

	local unit = Unit
	local hasTarget = false
	if not unit then unit = "player" end
	hasTarget = UnitExists("target")
	if unit then TargetUnit(unit) end
	UseContainerItem(bag, pos)
	if hasTarget then
		TargetLastTarget()
	else
		ClearTarget()
	end
end

function TMacros:GetItemInfoFromLink(Link)
	if not Link then return nil end 

	local _,_,id = string.find(Link, "item:(%d+)")
	if not id then return nil end 
	return GetItemInfo(id)
end

function TMacros:GetBagSlotItemName(BagId, SlotId)
	local name = self:GetBagSlotItemInfo(BagId, SlotId)
	return name
end

function TMacros:ItemReady(Item)
	if type(Item) ~= "string" then return end
	local bag, pos = self:GetBagItemSlot(Item)
	if not bag or not pos then return nil end
	local start = GetContainerItemCooldown(bag, pos)
	return start == 0
end

function TMacros:DropItemOnUnit(Item, Unit)
	if type(Item) ~= "string" or type(Unit) ~= "string" then return end
	local bag, pos = self:GetBagItemSlot(Item)
	if not bag or not pos then return end
	PickupContainerItem(bag, pos)
	DropItemOnUnit(Unit)
end

function TMacros:GetEquippedItemInfo(Slot)
	if type(Slot) ~= "number" or Slot < 1 or Slot > 23 then return nil end 

	local link = GetInventoryItemLink("player", Slot)
	if not link then return nil end

	return self:GetItemInfoFromLink(link)
end

function TMacros:EquipBagItemToInventory(Item, Slot)
	if type(Item) ~= "string" then return end
	if type(Slot) ~= "number" or Slot < 1 or Slot > 23 then return end

	local bag, pos = self:GetBagItemSlot(Item)
	if not bag or not pos then return end

	PickupContainerItem(bag, pos)
	EquipCursorItem(Slot)
end

function TMacros:ItemIsEquipped(Item, Slot)
	local equip = self:GetEquippedItemInfo(Slot)
	return equip == Item
end

function TMacros:InventoryReady(Unit, Slot)
	local start = GetInventoryItemCooldown(Unit, Slot)
	return start == 0
end

function TMacros:PlayerInventoryReady(Slot)
	return self:InventoryReady("player", Slot)
end